import {Injectable} from '@angular/core';
import {HttpClient, HttpHeaders} from '@angular/common/http';
import {map, Observable} from "rxjs";


@Injectable({
    providedIn: 'root'
})
/**
 * 商品列表服务
 */
export class ProductService {
    private baseUrl = 'http://localhost:8080/manager';

    constructor(private http: HttpClient) {
    }

    getCategoryVO(categoryId: string): Observable<any> {
        const url = `${this.baseUrl}/catalog?categoryid=${categoryId}`;
        return this.http.get<any>(url);
    }

    getProductVO(productId: string): Observable<any> {
        const url = `${this.baseUrl}/product?productid=${productId}`;
        return this.http.get<any>(url);
    }

    getItemVO(itemId: string): Observable<any> {
        const url = `${this.baseUrl}/item?itemid=${itemId}`;
        return this.http.get<any>(url);
    }

    updateCategory(editingProductId: string | undefined, editingCategoryId: string, editingName: string, editingDescription: string) {
        const url = `${this.baseUrl}/update/category`;
        const httpOptions = {
            headers: new HttpHeaders({'Content-Type': 'application/json'})
        };
        const body = {
            productId: editingProductId,
            categoryId: editingCategoryId,
            name: editingName,
            description: editingDescription
        };
        return this.http.post<any>(url, body, httpOptions);
    }

    getCategoryOptions() {
        return ['BIRDS', 'CATS', 'DOGS', 'FISH', 'REPTILES'];
    }

    getProductOptions(): Observable<string[]> {
        return this.getCategoryVO('ALL').pipe(
            map(category => {
                let options: string[] = [];
                for (let product of category?.data?.productList) {
                    if (!options.includes(product?.productId)) {
                        options.push(product?.productId);
                    }
                }
                return options;
            })
        );
    }

    getSupplierOptions() {
        const url = `${this.baseUrl}/suppliers`;
        return this.http.get<any>(url).pipe(
            map(result => {
                let options: string[] = [];
                for (let supplier of result?.data) {
                    if (!options.includes(supplier?.name)) {
                        options.push(supplier?.name);
                    }
                }
                return options;
            })
        );
    }

    getStatusOptions() {
        return ['P', 'N'];
    }

    getDescriptionOptions() {
        return this.getProductVO('ALL').pipe(
            map(product => {
                let options: string[] = [];
                for (let item of product?.data?.itemList) {
                    if (!options.includes(item?.attribute1)) {
                        options.push(item?.attribute1);
                    }
                }
                return options;
            })
        );
    }

    deleteProduct(productId: string) {
        const url = `${this.baseUrl}/delete/product?productid=${productId}`;
        return this.http.get<any>(url);
    }
}
